{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 访问限制\n",
    "\n",
    "给类建立私有变量: 可以把属性的名称前加两个 <font color=red>__</font>,即可把变量变为私有变量(private),此时只有类的内部可以访问.\n",
    "\n",
    "需要注意的是, 在Python中, 变量名类似<font color=red>__xxx__</font>, 也就是以双下划线开头, 并以双下划线结束, 是特殊变量, 特殊变量是可以直接访问的, 不是private变量, 所以, 不能用<font color=red>__name__</font>, <font color=red>__score__</font>这样的变量名.\n",
    "\n",
    "双下划线开头的实例变量是不是一定不能从外部访问呢？其实也不是。不能直接访问<font color=red>__name</font>是因为Python解释器对外把<font color=red>__name</font>变量改成了<font color=red>_Student__name</font>，所以，仍然可以通过<font color=red>_Student__name</font>来访问<font color=red>__name</font>变量。但是强烈建议你不要这么干，因为不同版本的Python解释器可能会把<font color=red>__name</font>改成不同的变量名。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Bart Simpson\n",
      "Bart Simpson\n"
     ]
    }
   ],
   "source": [
    "class Student(object):\n",
    "    def __init__(self, name, score):\n",
    "        self.__name = name    # 建立私有变量__name\n",
    "        self.__score = score  # 建立私有变量__score\n",
    "        \n",
    "    def print_score(self):\n",
    "        print(\"%s: %s\" % (self.__name, self.__score))\n",
    "        \n",
    "    def get_name(self):\n",
    "        return self.__name\n",
    "    \n",
    "    def get_score(self):\n",
    "        return self.__score\n",
    "    \n",
    "    def set_score(self, score):\n",
    "        if 0 <= score <= 100:\n",
    "            self.__score = score\n",
    "        else:\n",
    "            raise ValueError('bad score')\n",
    "        \n",
    "    \n",
    "        \n",
    "bart = Student('Bart Simpson', 59)\n",
    "bart.set_score(89) # 设置score数据\n",
    "print(bart.get_name()) # 访问name\n",
    "print(bart._Student__name) # \n",
    "# print(bart.__name) # 无法访问.__name属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": true,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
